﻿using SHomeWorkshop.LunarMind.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace SHomeWorkshop.LunarMind.LMETools
{
    public class DemoResourceCard : Window
    {
        public DemoResourceCard(GraphicDemonstrator demoWin, DemoResourceButton masterResourceButton)
        {
            this.Height = 400;
            this.Width = 500;
            this.WindowStyle = WindowStyle.ToolWindow;   // 注意：不能用普通窗口，否则最小化时易出问题。

            this.Owner = demoWin;
            this.masterResourceButton = masterResourceButton;

            try
            {
                var path = MasterResourceButton.ResourceInfo.AbsolutePath.ToLower();
                var htmlpath = "";
                if (path.EndsWith(".html"))
                {
                    htmlpath = path;
                }
                else if (path.EndsWith(".md"))
                {
                    htmlpath = path.Substring(0, path.Length - 3) + ".html";
                }

                if (htmlpath.EndsWith(".html"))
                {
                    if (System.IO.File.Exists(htmlpath))
                    {
                        this.frame = new Frame();
                        frame.Source = new Uri("file:///" + htmlpath);
                        this.Content = this.frame;
                    }
                    else
                    {
                        if (path.ToLower().EndsWith(".html"))
                        {
                            path = path.Substring(0, path.Length - 5) + ".md";
                        }

                        if (System.IO.File.Exists(path))
                        {
                            this.Content = new ICSharpCode.AvalonEdit.TextEditor()
                            {
                                Text = System.IO.File.ReadAllText(path, Encoding.UTF8),
                                Margin = new Thickness(10, 10, 10, 10),
                                FontSize = 36,
                                HorizontalAlignment = HorizontalAlignment.Stretch,
                                VerticalAlignment = VerticalAlignment.Stretch,
                                IsReadOnly = true,
                                WordWrap = true,
                            };
                        }
                    }
                }
                else
                {
                    // 暂时不支持音频、视频，只先支持图像
                    this.image = new Image();
                    this.image.Source = Globals.LoadBitmapImageFromFile(path);
                    this.Content = this.image;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            this.Closing += DemoResourceCard_Closing;
            this.PreviewMouseLeftButtonDown += DemoResourceCard_PreviewMouseLeftButtonDown; ;

            this.Activated += DemoResourceCard_Activated;

            this.SizeChanged += DemoResourceCard_SizeChanged;
        }

        private void DemoResourceCard_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                var demoWin = Window.GetWindow(this.MasterResourceButton) as GraphicDemonstrator;
                if (demoWin != null)
                {
                    if (demoWin is GraphicDemonstrator gwin)
                    {
                        var leftTop = demoWin.mainCanvas.PointToScreen(new Point(0, 0));
                        //var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                        this.Width = gwin.mainCanvas.ActualWidth;
                        this.Height = gwin.mainCanvas.ActualHeight;

                        this.WindowState = WindowState.Normal;
                        this.Left = leftTop.X;
                        this.Top = leftTop.Y;
                    }
                }
            }
            else if (this.WindowState == WindowState.Minimized)
            {
                this.MasterResourceButton.IsChecked = false;
            }
        }

        private void DemoResourceCard_Activated(object sender, EventArgs e)
        {
            if (this.Owner is GraphicDemonstrator gWin)
            {
                gWin.ActivedCard = this;
            }
        }

        private void DemoResourceCard_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = System.Windows.Visibility.Collapsed;
            this.MasterResourceButton.IsChecked = false;

            this.MasterResourceButton.SwitchLayoutPanels();
        }

        private void DemoResourceCard_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (this.WindowState == WindowState.Maximized)
                {
                    this.WindowState = WindowState.Normal;
                }
                else
                {
                    this.WindowState = WindowState.Maximized;
                }
            }
        }

        private Frame frame;

        private Image image;

        private DemoResourceButton masterResourceButton;

        public DemoResourceButton MasterResourceButton { get { return this.masterResourceButton; } }
    }
}
